Ext.define('Seq.app.ContainerController', {
    extend: 'Ext.app.ViewController',

    lastView: null,

    /**
     * @public
     * @param routes 支持多级路由
     * @returns {*}
     */
    setCurrent(container, routes, params) {
        let me = this, route = routes.shift(), view = me.viewOfRoute(route), refreshdata = true;
        let existedView = container.child(`container[routeId=${route}]`), newView;
        if (!existedView) {
            newView = Ext.widget(view, {
                hideMode: 'offsets',
                routeId: route
            })
        }
        if (!me.validateUser(newView || existedView)) { // 验证用户是否登录，如果未登录，释放新建的窗体
            newView && newView.destroy();
            return;
        }
        let containerLayout = container.getLayout();
        if (newView) { // 有新窗体
            if (!newView.isWindow) {// 不是Window
                Ext.suspendLayouts();
                try {
                    containerLayout.setActiveItem(container.add(newView));
                } finally {
                    Ext.resumeLayouts(true);
                }
            }
        } else { // 没有新窗机，那一定有存在的窗体
            let view = containerLayout.getActiveItem();
            if (existedView.routeId !== view.routeId) {
                containerLayout.setActiveItem(existedView);
            } else {
                refreshdata = false; // error为window，回退后不能刷新页面
            }
            newView = existedView;
        }
        if (0 < routes.length) { // 如果还存在下级页面，继续构建下级页面
            let controller = newView.getController();
            if (controller && controller.setCurrent) {
                controller.setCurrent(newView, routes, params);
                me.afterRouteChange(newView, route);
                return newView;
            }
        }
        view = me.lastView;
        if (view && view.isWindow) { // 释放Window窗体，否则会保存旧值
            view.ownerCt && view.ownerCt.remove(view);
            view.destroy();
        }
        refreshdata && newView.fireEvent('refreshdata', newView, null, params, {}); // 发送页面刷新的消息
        me.lastView = newView;
        me.afterRouteChange(newView, route);
        return newView;
    },

    /**
     * @private
     * 验证用户是否登录
     * @param view 访问的窗体对象
     * @returns {*}
     */
    validateUser(view) {
        let valid = Ext.isEmpty(view.validateUser) ? true : view.validateUser;
        if (!valid) {
            return true;
        }
        let vm = Ext.getApplication().getMainView().getViewModel(), user = vm.get('user');
        if (!user) {
            return this.readUser();
        }
        return user;
    },

    /**
     * @private
     * 获取登录用户信息
     * @returns {*}
     */
    readUser() {
        let user;
        Seq.Ajax.request({
            url: 'rest/oauth/sign/info',
            async: false,
            fnSuccess(options, result) {
                user = result.data;
                let app = Ext.getApplication();
                app.afterLogin && app.afterLogin(user);
            }
        });
        return user;
    },

    /**
     * @protected
     * 返回route对应的视图类型
     * @param route
     * @returns {*}
     */
    viewOfRoute(route) {
        return route;
    },

    /**
     * @protected
     * after route change
     * @param view 新的窗体对象
     * @param route 路由编号
     */
    afterRouteChange(view, route) {
    }

})
;